package PVZ;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public abstract class Senobject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    protected int state = LIVE;
    int width;
    int height;
    int x;
    int y;
    int life;

    public Senobject(int width, int height, int x, int y, int life) {
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.life = life;
    }

    public Senobject(int width, int height, int life) {//僵尸类
        this.width = width;
        this.height = height;
        Random ran = new Random();
        this.y = ran.nextInt(5) * 92 + 171 - this.height;
        this.x = 900 + this.width;
        this.life = life;
    }

    public abstract ImageIcon getImage();

    public void paintImage(Graphics g) {
        getImage().paintIcon(null, g, x, y);
    }

    public void step() {
    }

    //判断是否是活着
    public boolean isLive(){
        return state==LIVE;
    }
    //判断是否为死了
    public boolean isDead(){
        return state==DEAD;
    }

    public Senobject shootBullet() {
        int x = this.x + this.width-15;
        int y = this.y ;
        if (this instanceof Repeater) {
            return new RepeaterBullet(x,y,height);
        } else if (this instanceof Snowpea) {
            return new SnowPeashooterBullet(x,y,height);
        }else if (this instanceof Peashooter){
            return new RepeaterBullet(x,y,height);
        } else{
            return null;
        }
    }

    /*子弹与僵尸碰撞检测*/
    public boolean isHit(Senobject other) {
        //假设this为僵尸 other为子弹
        int x = other.x + other.width;//子弹的x+子弹的宽
        int x1 = this.x;              //僵尸的x
        int x2 = this.x + this.width; //僵尸的x+僵尸的宽
        int y1=0;
        if (other instanceof Peashooter){
            plantheight ph=(plantheight)other;
            y1=other.y-ph.getPlanth();
        }
        int y2=this.y-this.height;
        return x >= x1 && x <= x2 && y1==y2;
    }

    /*僵尸与植物碰撞检测*/
    public boolean isHit1(Senobject other) {
        //假设this为僵尸 other为植物
        int x = this.x;
        int x1 = other.x;
        int x2 = other.x + other.width;
        return x >= x1 && x <= x2;
    }
    //减命
    public void loseLife(){};
    //对象死亡
    public void goDead() {
        state = DEAD;
    }
}
